#include "hplccommonpage.h"
#include <QDebug>
#include "modulemanager.h"
#include "modulehplcpump.h"
#include "modulehplcpumpparamiddefine.h"
#include "modulehplccmdid.h"
#include <QCloseEvent>
#include "modulehplccmdid.h"

HplcCommonPage::HplcCommonPage(QWidget *parent)
    : QWidget (parent)
{
    setWindowTitle(tr("Hplc Common Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    ModuleHplcPump *mod = static_cast<ModuleHplcPump*>(moduleManager->find(ModuleID_HPLC_PumpA));
    if (mod == nullptr)
        return;
    connect(mod, &ModuleHplcPump::parseCompleted,
            this, &HplcCommonPage::onParseCompleted);

    // 设备地址
    m_lbDeviceAddress = new QLabel(tr("DeviceAddress"));
    m_leDeviceAddress = new QLineEdit();
    m_pbSetDeviceAddress = new QPushButton(tr("Set"));
    connect(m_pbSetDeviceAddress, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 address = m_leDeviceAddress->text().toUInt();
        var.setValue(address);
        mod->setProtocolCmd(hplc::CommonCmdID_SetDeviceAddress, var);
    });
    m_pbGetDeviceAddress = new QPushButton(tr("Get"));
    connect(m_pbGetDeviceAddress, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetDeviceAddress, var);
    });
    hblDeviceAddress = new QHBoxLayout();
    hblDeviceAddress->addWidget(m_lbDeviceAddress);
    hblDeviceAddress->addWidget(m_leDeviceAddress);
    hblDeviceAddress->addWidget(m_pbSetDeviceAddress);
    hblDeviceAddress->addWidget(m_pbGetDeviceAddress);

    // 软件版本
    m_lbSoftwareVersion = new QLabel(tr("SoftwareVersion"));
    m_leSoftwareVersion = new QLineEdit();
    m_pbSetSoftwareVersion = new QPushButton(tr("Set"));
    m_pbSetSoftwareVersion->setEnabled(false);
    m_pbGetSoftwareVersion = new QPushButton(tr("Get"));
    connect(m_pbGetSoftwareVersion, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetSoftwareVersion, var);
    });
    hblSoftwareVersion = new QHBoxLayout();
    hblSoftwareVersion->addWidget(m_lbSoftwareVersion);
    hblSoftwareVersion->addWidget(m_leSoftwareVersion);
    hblSoftwareVersion->addWidget(m_pbSetSoftwareVersion);
    hblSoftwareVersion->addWidget(m_pbGetSoftwareVersion);

    // 硬件版本
    m_lbHardwareVersion = new QLabel(tr("HardwareVersion"));
    m_leHardwareVersion = new QLineEdit();
    m_pbSetHardwareVersion = new QPushButton(tr("Set"));
    m_pbSetHardwareVersion->setEnabled(false);
    m_pbGetHardwareVersion = new QPushButton(tr("Get"));
    connect(m_pbGetHardwareVersion, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetHardwareVersion, var);
    });
    hblHardwareVersion = new QHBoxLayout();
    hblHardwareVersion->addWidget(m_lbHardwareVersion);
    hblHardwareVersion->addWidget(m_leHardwareVersion);
    hblHardwareVersion->addWidget(m_pbSetHardwareVersion);
    hblHardwareVersion->addWidget(m_pbGetHardwareVersion);

    // 生产日期
    m_lbProductDate = new QLabel(tr("ProductDate"));
    m_leProductDate = new QLineEdit();
    m_pbSetProductDate = new QPushButton(tr("Set"));
    m_pbSetProductDate->setEnabled(false);
    m_pbGetProductDate = new QPushButton(tr("Get"));
    connect(m_pbGetProductDate, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetProductDate, var);
    });
    hblProductDate = new QHBoxLayout();
    hblProductDate->addWidget(m_lbProductDate);
    hblProductDate->addWidget(m_leProductDate);
    hblProductDate->addWidget(m_pbSetProductDate);
    hblProductDate->addWidget(m_pbGetProductDate);

    // 序列号
    m_lbSerialNumber = new QLabel(tr("SerialNumber"));
    m_leSerialNumber = new QLineEdit();
    m_pbSetSerialNumber = new QPushButton(tr("Set"));
    m_pbSetSerialNumber->setEnabled(false);
    m_pbGetSerialNumber = new QPushButton(tr("Get"));
    connect(m_pbGetSerialNumber, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetSerialNumber, var);
    });
    hblSerialNumber = new QHBoxLayout();
    hblSerialNumber->addWidget(m_lbSerialNumber);
    hblSerialNumber->addWidget(m_leSerialNumber);
    hblSerialNumber->addWidget(m_pbSetSerialNumber);
    hblSerialNumber->addWidget(m_pbGetSerialNumber);

    // 型号
    m_lbModel = new QLabel(tr("Model"));
    m_leModel = new QLineEdit();
    m_pbSetModel = new QPushButton(tr("Set"));
    m_pbSetModel->setEnabled(false);
    m_pbGetModel = new QPushButton(tr("Get"));
    connect(m_pbGetModel, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetModel, var);
    });
    hblModel = new QHBoxLayout();
    hblModel->addWidget(m_lbModel);
    hblModel->addWidget(m_leModel);
    hblModel->addWidget(m_pbSetModel);
    hblModel->addWidget(m_pbGetModel);

    // 使用时间
    m_lbUsedTime = new QLabel(tr("UsedTime"));
    m_leUsedTime = new QLineEdit();
    m_pbSetUsedTime = new QPushButton(tr("Set"));
    m_pbSetUsedTime->setEnabled(false);
    m_pbGetUsedTime = new QPushButton(tr("Get"));
    connect(m_pbGetUsedTime, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetUsedTime, var);
    });
    hblUsedTime = new QHBoxLayout();
    hblUsedTime->addWidget(m_lbUsedTime);
    hblUsedTime->addWidget(m_leUsedTime);
    hblUsedTime->addWidget(m_pbSetUsedTime);
    hblUsedTime->addWidget(m_pbGetUsedTime);

    // 同步时间
    m_lbSyncTime = new QLabel(tr("SyncTime"));
    m_leSyncTime = new QLineEdit();
    m_pbSetSyncTime = new QPushButton(tr("Set"));
    m_pbSetSyncTime->setEnabled(false);
    m_pbGetSyncTime = new QPushButton(tr("Get"));
    connect(m_pbGetSyncTime, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetSyncTime, var);
    });
    hblSyncTime = new QHBoxLayout();
    hblSyncTime->addWidget(m_lbSyncTime);
    hblSyncTime->addWidget(m_leSyncTime);
    hblSyncTime->addWidget(m_pbSetSyncTime);
    hblSyncTime->addWidget(m_pbGetSyncTime);

    // 输入点
    m_lbInput = new QLabel(tr("Input"));
    m_leInputPos = new QLineEdit();
    m_leInputValue = new QLineEdit();
    m_pbSetInput = new QPushButton(tr("Set"));
    m_pbSetInput->setEnabled(false);
    m_pbGetInput = new QPushButton(tr("Get"));
    connect(m_pbGetInput, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_leInputPos->text().toUInt());
        mod->getProtocolCmd(hplc::CommonCmdID_GetInput, var);
    });
    hblInput = new QHBoxLayout();
    hblInput->addWidget(m_lbInput);
    hblInput->addWidget(m_leInputPos);
    hblInput->addWidget(m_leInputValue);
    hblInput->addWidget(m_pbSetInput);
    hblInput->addWidget(m_pbGetInput);

    // 输出点
    m_lbOutput = new QLabel(tr("Output"));
    m_leOutputPos = new QLineEdit();
    m_leOutputValue = new QLineEdit();
    m_pbSetOutput = new QPushButton(tr("Set"));
    connect(m_pbSetOutput, &QPushButton::clicked, this, [=](){
        QVariant var;
        hplc::Output output;
        output.number = m_leOutputPos->text().toUInt();
        output.level = m_leOutputValue->text().toUInt();
        var.setValue(output);
        mod->setProtocolCmd(hplc::CommonCmdID_SetOutput, var);
    });
    m_pbGetOutput = new QPushButton(tr("Get"));
    connect(m_pbGetOutput, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_leOutputPos->text().toUInt());
        mod->getProtocolCmd(hplc::CommonCmdID_GetOutput, var);
    });
    hblOutput = new QHBoxLayout();
    hblOutput->addWidget(m_lbOutput);
    hblOutput->addWidget(m_leOutputPos);
    hblOutput->addWidget(m_leOutputValue);
    hblOutput->addWidget(m_pbSetOutput);
    hblOutput->addWidget(m_pbGetOutput);

    // 心跳包
    m_lbTick = new QLabel(tr("Tick"));
    m_leTick = new QLineEdit();
    m_pbSetTick = new QPushButton(tr("Set"));
    connect(m_pbSetTick, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_SetTick, var);
    });
    m_pbGetTick = new QPushButton(tr("Get"));
    m_pbGetTick->setEnabled(false);
    hblTick = new QHBoxLayout();
    hblTick->addWidget(m_lbTick);
    hblTick->addWidget(m_leTick);
    hblTick->addWidget(m_pbSetTick);
    hblTick->addWidget(m_pbGetTick);

    // 心跳包周期
    m_lbTickPeriod = new QLabel(tr("TickPeriod"));
    m_leTickPeriod = new QLineEdit();
    m_pbSetTickPeriod = new QPushButton(tr("Set"));
    connect(m_pbSetTickPeriod, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 period = m_leTickPeriod->text().toUInt();
        var.setValue(period);
        mod->setProtocolCmd(hplc::CommonCmdID_SetTickPeriod, var);
    });
    m_pbGetTickPeriod = new QPushButton(tr("Get"));
    connect(m_pbGetTickPeriod, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetTickPeriod, var);
    });
    hblTickPeriod = new QHBoxLayout();
    hblTickPeriod->addWidget(m_lbTickPeriod);
    hblTickPeriod->addWidget(m_leTickPeriod);
    hblTickPeriod->addWidget(m_pbSetTickPeriod);
    hblTickPeriod->addWidget(m_pbGetTickPeriod);

    // 系统错误
    m_lbError = new QLabel(tr("Error"));
    m_leError = new QLineEdit();
    m_pbSetError = new QPushButton(tr("Set"));
    m_pbSetError->setEnabled(false);
    m_pbGetError = new QPushButton(tr("Get"));
    connect(m_pbGetError, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetSystemError, var);
    });
    hblError = new QHBoxLayout();
    hblError->addWidget(m_lbError);
    hblError->addWidget(m_leError);
    hblError->addWidget(m_pbSetError);
    hblError->addWidget(m_pbGetError);

    // 系统错误上传频率
    m_lbErrorUploadFreq = new QLabel(tr("ErrorUploadFreq"));
    m_leErrorUploadFreq = new QLineEdit();
    m_pbSetErrorUploadFreq = new QPushButton(tr("Set"));
    connect(m_pbSetErrorUploadFreq, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 freq = m_leErrorUploadFreq->text().toUInt();
        var.setValue(freq);
        mod->setProtocolCmd(hplc::CommonCmdID_SetSystemErrorUploadFreq, var);
    });
    m_pbGetErrorUploadFreq = new QPushButton(tr("Get"));
    connect(m_pbGetErrorUploadFreq, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonCmdID_GetSystemErrorUploadFreq, var);
    });
    hblErrorUploadFreq = new QHBoxLayout();
    hblErrorUploadFreq->addWidget(m_lbErrorUploadFreq);
    hblErrorUploadFreq->addWidget(m_leErrorUploadFreq);
    hblErrorUploadFreq->addWidget(m_pbSetErrorUploadFreq);
    hblErrorUploadFreq->addWidget(m_pbGetErrorUploadFreq);

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(hblDeviceAddress);
    vbl->addLayout(hblSoftwareVersion);
    vbl->addLayout(hblHardwareVersion);
    vbl->addLayout(hblProductDate);
    vbl->addLayout(hblSerialNumber);
    vbl->addLayout(hblModel);
    vbl->addLayout(hblUsedTime);
    vbl->addLayout(hblSyncTime);
    vbl->addLayout(hblInput);
    vbl->addLayout(hblOutput);
    vbl->addLayout(hblTick);
    vbl->addLayout(hblTickPeriod);
    vbl->addLayout(hblError);
    vbl->addLayout(hblErrorUploadFreq);
    setLayout(vbl);
}

HplcCommonPage::~HplcCommonPage()
{

}

void HplcCommonPage::closeEvent(QCloseEvent *event)
{
    disconnect();
    event->accept();
}

void HplcCommonPage::onParseCompleted(int id, const QVariant &var)
{
    switch (id)
    {
        case hplc::CommonCmdID_SetDeviceAddress:
        {
            quint8 address = var.toUInt();
            m_leDeviceAddress->setText(QString::number(address));
        }
        break;
        case hplc::CommonCmdID_SetSoftwareVersion:
        {
            QString str = var.toString();
            m_leSoftwareVersion->setText(str);
        }
        break;
        case hplc::CommonCmdID_SetHardwareVersion:
        {
            QString str = var.toString();
            m_leHardwareVersion->setText(str);
        }
        break;
        case hplc::CommonCmdID_SetProductDate:
        {
            QString str = var.toString();
            m_leProductDate->setText(str);
        }
        break;
        case hplc::CommonCmdID_SetSerialNumber:
        {
            QString str = var.toString();
            m_leSerialNumber->setText(str);
        }
        break;
        case hplc::CommonCmdID_SetModel:
        {
            QString str = var.toString();
            m_leModel->setText(str);
        }
        break;
        case hplc::CommonCmdID_SetUsedTime:
        {
            quint32 time = var.toUInt();
            m_leUsedTime->setText(QString::number(time));
        }
        break;
        case hplc::CommonCmdID_SetSyncTime:
        {
            quint32 time = var.toUInt();
            m_leSyncTime->setText(QString::number(time));
        }
        break;
        case hplc::CommonCmdID_SetInput:
        {
            hplc::Input input = var.value<hplc::Input>();
            m_leInputPos->setText(QString::number(input.number));
            m_leInputValue->setText(QString::number(input.level));
        }
        break;
        case hplc::CommonCmdID_SetOutput:
        {
            hplc::Output output = var.value<hplc::Output>();
            m_leOutputPos->setText(QString::number(output.number));
            m_leOutputValue->setText(QString::number(output.level));
        }
        break;
        case hplc::CommonCmdID_SetTick:
        {
            m_leTick->setText("1");
        }
        break;
        case hplc::CommonCmdID_SetTickPeriod:
        {
            quint8 period = var.toUInt();
            m_leTickPeriod->setText(QString::number(period));
        }
        break;
        case hplc::CommonCmdID_SetSystemError:
        {
            quint8 code = var.toUInt();
            m_leError->setText(QString::number(code));
        }
        break;
        case hplc::CommonCmdID_SetSystemErrorUploadFreq:
        {
            quint8 freq = var.toUInt();
            m_leErrorUploadFreq->setText(QString::number(freq));
        }
        break;

        default:
        break;
    }
}
